package com.dfrz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dfrz.entity.Utilrecommend;
import com.dfrz.entity.Video;
import com.dfrz.entity.VideoDetail;
import com.dfrz.mapper.IVideoMapper;
import com.dfrz.mapper.VideoDetailMapper;
import com.dfrz.service.VideoService;
import com.dfrz.utils.AliOSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 29877
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class VideoServiceImpl  implements VideoService {
    @Resource
    private IVideoMapper iVideoMapper;

    @Resource
    private VideoDetailMapper detailMapper;

    //发布视频
    @Override
    public int addVideoM(Video video, MultipartFile videoImg) throws Exception {
        String videoUrl = AliOSSUtil.uploadFile(videoImg);
        video.setVideoUrl(videoUrl);
        return iVideoMapper.insert(video);
    }

    //根据每页需要的条数跟页码算出起始索引
    @Override
    public List<Video> selectLimit(Integer page,Integer num) {
        Integer index=(page-1)*num;
        return iVideoMapper.selectLimit(index,num);
    }

    @Override
    public Video selectvideobyid(Integer id) {
        return iVideoMapper.selectvideobyid(id);
    }

    @Override
    public List<Video> selectorderbyscore() {
        return iVideoMapper.selectorderbyscore();
    }

    @Override
    public List<Utilrecommend> seleltrecommend() {
        int[] arr={1,2,3,4};
        List<Utilrecommend> list=new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            list.add(iVideoMapper.selectrecommend(arr[i]));
        }
        return list;
    }

    @Override
    public List<Video> selectrandom() {
        Random r=new Random();
        Set<Integer> set=new HashSet<>();
        while (set.size()<14){
            set.add(r.nextInt(40));
        }
        List<Video> videos=new ArrayList<>();
        Iterator<Integer> iterator=set.iterator();
        while (iterator.hasNext()){
            videos.add(iVideoMapper.selectvideobyid(iterator.next()));
        }
        return videos;
    }

    @Override
    public List<Video> selecthot() {
        return iVideoMapper.selectorderbyscore2();
    }

    //管理员模糊查询视频为视频添加续集
    @Override
    public List<Video> selectvideoByName(String videoTitle) {
        Wrapper<Video> wrapper = new QueryWrapper<Video>()
                .like("video_title",videoTitle);
        List<Video> videos = iVideoMapper.selectList(wrapper);
        return videos;
    }

    //普通用户发布视频
    @Override
    public Integer addVideoU(Video videoInfo, MultipartFile video, MultipartFile videoImg) throws Exception {
        iVideoMapper.insert(videoInfo);
        String imgUrl = AliOSSUtil.uploadFile(videoImg);
        String videoUrl = AliOSSUtil.upLoadPartFile(video);
        Integer checkStatus = 0;
        Integer detailType = 0;
        VideoDetail videoDetail = new VideoDetail(checkStatus,detailType,videoInfo.getVideoId(),videoUrl,imgUrl);
        return detailMapper.insert(videoDetail);
    }


    @Override
    public List<Video> searchVideo(String word) {
        word="%"+word+"%";
       return iVideoMapper.searchvideo(word);
    }

    @Override
    public Map<String,Object> getVideoInfoByEdit() {
        List<Video> videos1 = iVideoMapper.selectEdidCount();
        long countPage = (long) Math.ceil(videos1.size()/10.0);
        List<Video> videos = iVideoMapper.selectPageEdit(0,10);
        Map<String,Object> map =new HashMap<>();
        map.put("countPage", countPage);
        map.put("videos", videos);
        return map;
    }

    @Override
    public List<Video> getPageingEdit(Integer page) {
        page=(page-1)*10;
        List<Video> videos = iVideoMapper.selectPageEdit(page,10);
        return videos;
    }

    @Override
    public void deleteVideoEdit(Integer videoId) {
        QueryWrapper<Video> eq = new QueryWrapper<Video>()
                .eq("video_id", videoId);
        iVideoMapper.delete(eq);
    }

    @Override
    public void upVideoInfo(MultipartFile videoImg, Video video) throws Exception {
        System.out.println(video);
        if(videoImg !=null){
            String videoUrl = AliOSSUtil.uploadFile(videoImg);
            video.setVideoUrl(videoUrl);
        }
        System.out.println(video);
        iVideoMapper.updateById(video);
    }
}
